/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: StaffController.java 47 2012-09-20 06:31:35Z zhuxiaopeng $
 * $Revision: 47 $
 * $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 * $Author: zhuxiaopeng $
 */
package daily.web.staff;

import static zhu.framework.web.WebUtility.*;

import java.util.*;

import javax.enterprise.context.*;
import javax.inject.*;

import zhu.framework.service.*;
import zhu.framework.web.*;
import zhu.framework.web.jsf.*;
import daily.entity.*;
import daily.service.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 47 $ - $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 */
@Named
@RequestScoped
public class StaffGroupController extends ConversationalController {

	private enum ActionOutCome {
		EDIT_GROUP, SAVE, CANCEL, SET_PERMISSION, EDIT, DELETE_GROUP
	}

	public String deleteStaffGroup() {
		return ActionOutCome.DELETE_GROUP.toString();
	}

	public String editStaffGroup() {
		return ActionOutCome.EDIT_GROUP.toString();
	}

	public String createStaffGroup() {
		final StaffGroup selectStaffGroup = new StaffGroup();
		staffGroupConversation.setSelectStaffGroup(selectStaffGroup);
		return ActionOutCome.EDIT_GROUP.toString();
	}

	public String delete() {
		try {
			staffGroupService.deleteStaffGroup(staffGroupConversation.getSelectStaffGroup());
			staffGroupConversation.setStaffGroupList(null);
		} catch (final BusinessException e) {
			getWebRequestContext().addMessages(WebMessage.valueOf(e.getMessages()));
			return null;
		}
		return ActionOutCome.SAVE.toString();
	}

	public String saveStaffGroup() {
		try {
			staffGroupService.saveStaffGroup(staffGroupConversation.getSelectStaffGroup());
			staffGroupConversation.setStaffGroupList(null);
		} catch (final BusinessException e) {
			getWebRequestContext().addMessages(WebMessage.valueOf(e.getMessages()));
			return null;
		}
		return ActionOutCome.SAVE.toString();
	}

	public String setPermission() {
		final StaffGroup staffGroupPermission = staffGroupService.getStaffGroupPermission(staffGroupConversation.getSelectStaffGroup());
		staffGroupConversation.setSelectStaffGroup(staffGroupPermission);
		return ActionOutCome.SET_PERMISSION.toString();
	}

	public String editPermission() {
		return ActionOutCome.EDIT.toString();
	}

	public String cancel() {
		return ActionOutCome.CANCEL.toString();
	}

	public List<StaffGroup> getStaffGroupList() {
		List<StaffGroup> staffGroupList = staffGroupConversation.getStaffGroupList();
		if (staffGroupList == null) {
			staffGroupList = staffGroupService.getStaffGroupList();
			staffGroupConversation.setStaffGroupList(staffGroupList);
		}
		return staffGroupList;
	}

	@Inject
	private StaffGroupConversation staffGroupConversation;

	@Inject
	private StaffGroupServiceImpl staffGroupService;
}
